package com.eleventwelve.iris.entity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.opentides.bean.BaseEntity;
import org.opentides.bean.SystemCodes;

/**
 * 
 * @author Ashley
 *
 */
@Entity
@Table(name="PATIENT_MEDICAL_PROGRAM")
public class PatientMedicalProgram extends BaseEntity{
	private static final long serialVersionUID = 3341099857799607050L;

	@ManyToOne
	@JoinColumn(name="PATIENT_ID")
	private Patient patient;
	
	@Column(name="NAME")
	private String name;
	
	@Lob
	@Column(name="DESCRIPTION")
	private String description;
	
	@Lob
	@Column(name="GOAL")
	private String goal;
	
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="CREATOR")
	private Staff createdBy;
	
	@Lob
	@Column(name="RESULT_SUMMARY")
	private String resultSummary;
	
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="RESULT_STATUS")
	private SystemCodes resultStatus;

	@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "patientMedicalProgram")
	private List<MedicalProgramTreatment> medicalProgramTreatments;
	
	public SystemCodes getResultStatus() {
		return resultStatus;
	}

	public void setResultStatus(SystemCodes resultStatus) {
		this.resultStatus = resultStatus;
	}

	public Patient getPatient() {
		return patient;
	}

	public void setPatient(Patient patient) {
		this.patient = patient;
	}

	public String getResultSummary() {
		return resultSummary;
	}

	public void setResultSummary(String resultSummary) {
		this.resultSummary = resultSummary;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getGoal() {
		return goal;
	}

	public void setGoal(String goal) {
		this.goal = goal;
	}

	public List<MedicalProgramTreatment> getMedicalProgramTreatments() {
		return medicalProgramTreatments;
	}

	public void setMedicalProgramTreatments(List<MedicalProgramTreatment> medicalProgramTreatments) {
		this.medicalProgramTreatments = medicalProgramTreatments;
	}

	public Staff getCreatedBy() {
		return createdBy;
	}

	public void setCreatedBy(Staff createdBy) {
		this.createdBy = createdBy;
	}
	
	public int getTreatmentCompletionCount(){
		int completedCounter = 0;
		for (MedicalProgramTreatment treatment : medicalProgramTreatments) {
			if(treatment.getIsCompleted()){
				completedCounter++;
			}
		}
		return completedCounter;
	}
	
	public List<MedicalProgramTreatment> getUpcomingTreatments(){
		List<MedicalProgramTreatment> treatments = new ArrayList<MedicalProgramTreatment>();
		
		for (MedicalProgramTreatment medicalProgramTreatment : medicalProgramTreatments) {
			if(!medicalProgramTreatment.getIsCompleted()){
				treatments.add(medicalProgramTreatment);
			}
		}
		
		return treatments;
	}
	
	public List<MedicalProgramTreatment> getCompletedTreatments(){
		List<MedicalProgramTreatment> treatments = new ArrayList<MedicalProgramTreatment>();
		
		for (MedicalProgramTreatment medicalProgramTreatment : medicalProgramTreatments) {
			if(medicalProgramTreatment.getIsCompleted()){
				treatments.add(medicalProgramTreatment);
			}
		}
		
		if(treatments!=null && treatments.size()>0){
			List<MedicalProgramTreatment> sortedTreatments = new ArrayList<MedicalProgramTreatment>();
			sortedTreatments.addAll(treatments);
			Collections.sort(sortedTreatments, new Comparator<MedicalProgramTreatment>() {
			    public int compare(MedicalProgramTreatment t1, MedicalProgramTreatment t2) {
			    	if(t1.getCreateDate()!=null || t2.getCreateDate()!=null){
			    		return t2.getTreatmentDate().compareTo(t1.getTreatmentDate());
			    	}
			    	return 0;
			    }
			});
			
			return sortedTreatments;
		}
		
		return treatments;
	}
	
}
